﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;
using eProject_SEM3_SecurityServices.DataAccess;
using System.Data;

namespace eProject_SEM3_SecurityServices
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        BDepartment bd = null;
        BService bs = null;
        BTaskOfDepartment btod = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            bd = new BDepartment();
            bs = new BService();
            btod = new BTaskOfDepartment();
            MultiView1.SetActiveView(View1);

            if (IsPostBack == false)
            {
                BinDatagrvDepartment();
                DataBindgrvTaskOfDepartment();
                                             
            }
            DataBindDepartment();
            DataBindService();
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bd = new BDepartment();
            int departmentID = 0;
           
                departmentID = bd.getDepartmentList().Count() + 1;
            
            
            ODepartment od = new ODepartment();
            od.DepartmentID = departmentID;
            od.DepartmentName = txtDepName.Text;
            od.DepartmentDescription = txtDepDescription.Text; 
           
            bd.AddDepartment(od);            
            BinDatagrvDepartment();
            DataBindDepartment();
        }

        public void BinDatagrvDepartment()
        {
            grvDepartment.DataSource = bd.getDepartmentList();
            grvDepartment.DataBind();
        }       

        protected void grvDepartment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int departmentId = 0;
                if (e.CommandName == "EmployeeOfDepartment")
                {
                        int i = Convert.ToInt32(e.CommandArgument.ToString());
                        departmentId = int.Parse(grvDepartment.Rows[i].Cells[0].Text);
                        bindDataTogrvEmployeeOfDepartment(departmentId);
                        MultiView1.SetActiveView(View2);                                          
                }            
        }
        public void bindDataTogrvEmployeeOfDepartment(int departmentId)
        {
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@DepartmetID",departmentId)
            };

            DataTable table = DConnectSQL.ExecuteParamerizedSelectCommand("getListEmployeeByDepartmentID", CommandType.StoredProcedure, para);
            grvEmployeeOfDepartment.DataSource = table;
            grvEmployeeOfDepartment.DataBind();
        }

        protected void grvDepartment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvDepartment.EditIndex = e.NewEditIndex;
            BinDatagrvDepartment();
            //BinDataddlFunctionOfDepartment(); 
            MultiView1.SetActiveView(View1);
        }

        protected void grvDepartment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvDepartment.EditIndex = -1;
            BinDatagrvDepartment();
            //BinDataddlFunctionOfDepartment();
            MultiView1.SetActiveView(View1);
        }

        protected void grvDepartment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox departmentName = (TextBox)grvDepartment.Rows[e.RowIndex].FindControl("DepartmentName");
            TextBox DepartmentDescription = (TextBox)grvDepartment.Rows[e.RowIndex].FindControl("DepartmentDescription");            
            ODepartment od = new ODepartment();
            od.DepartmentID = int.Parse(grvDepartment.Rows[e.RowIndex].Cells[0].Text);
            od.DepartmentName = departmentName.Text;
            od.DepartmentDescription = DepartmentDescription.Text;

            bd.UpdateDepartment(od);
            BinDatagrvDepartment();

        }

        public void DataBindDepartment()
        {
            ddlDepartment.DataSource =  bd.getDepartmentList();
            ddlDepartment.DataTextField = "departmentName";
            ddlDepartment.DataValueField = "departmentId";
            ddlDepartment.DataBind();
        }
        public void DataBindService()
        {
            ddtService.DataSource = bs.getServiceList();
            ddtService.DataTextField = "serviceName";
            ddtService.DataValueField = "serviceId";
            ddtService.DataBind();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            OTaskOfDepartment otod = new OTaskOfDepartment();
            otod.DepartmentId = int.Parse(ddlDepartment.SelectedValue);
            otod.ServiceId = int.Parse(ddtService.SelectedValue);

            btod.NewTaskDepartmentAdd(otod);
            DataBindgrvTaskOfDepartment();
        }
        public void DataBindgrvTaskOfDepartment()
        {
            grvFunctionOfDepartment.DataSource = btod.getListFunctionOfDepartment();
            grvFunctionOfDepartment.DataBind();
        }

       
    }
}